package com.ygo.feihua;
import android.support.v7.app.*;
import android.os.*;
import android.view.View.*;
import android.view.*;
import cn.bmob.v3.*;
import org.json.*;
import cn.bmob.v3.listener.*;
import cn.bmob.v3.exception.*;
import android.support.v7.widget.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.*;
import android.widget.AdapterView;


public class PushManagement extends AppCompatActivity
{

	Button push_push;
	EditText push_message,push_uri,push_title;
	Spinner push_type;
	Gongju gj;
	int type;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.push_management);
		initView();
		
	}

	private void initView()
	{
		push_push=(Button) findViewById(R.id.push_push);
		push_message=(EditText) findViewById(R.id.push_message);
		push_title=(EditText) findViewById(R.id.push_title);
		push_uri=(EditText) findViewById(R.id.push_uri);
		push_type=(Spinner) findViewById(R.id.push_type);
		
		
		gj=Gongju.getdx(this);
		gj.cshToolbar((Toolbar)findViewById(R.id.toolbar),"推送管理");
		
		push_type.setOnItemSelectedListener(new OnItemSelectedListener(){

				@Override
				public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
				{
					
						type=p3;
					// TODO: Implement this method
				}

				@Override
				public void onNothingSelected(AdapterView<?> p1)
				{
					// TODO: Implement this method
				}
			});		
			
			
		push_push.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String message=push_message.getText().toString().trim();
					String title=push_title.getText().toString();
					String uri=push_uri.getText().toString();
					if(!title.equals("")&&!message.equals("")&&!uri.equals("")){
					BmobPushManager bp=new BmobPushManager();
					try
					{
						bp.pushMessageAll(getPushJson(title,message, uri), new PushListener(){

								@Override
								public void done(BmobException p1)
								{
									if(p1==null){
										gj.show("推送成功");
									}else{
										gj.show("推送失败,原因为"+p1);
									}
									// TODO: Implement this method
								}
							});
					}
					catch (JSONException e)
					{
						gj.show("解析错误,原因为"+e);
						
						
					}

					}else{
						gj.show("信息未填完整");
					}
					
					
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
	}
	
	private JSONObject getPushJson(String title,String message,String uri) throws JSONException{
		
		
			return new JSONObject("{ \"type\":"+type+", \"title\":\""+title+"\", \"message\":\"" 
					+ message
					+"\", \"intenturi\":\"" + uri + "\" }");
			

			
	}
}
